package com.yc.cloud.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.service.AvatarsSettingService;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.entity.AvatarsSetting;
import com.yc.cloud.mapper.AvatarsSettingMapper;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数字人设置 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-04-10
 */
@Service
public class AvatarsSettingServiceImpl extends ServiceImpl<AvatarsSettingMapper, AvatarsSetting> implements AvatarsSettingService {


    @Override
    public IPage<AvatarsSetting> list(BaseQueryParam queryParam) {
        val query = new QueryWrapper<AvatarsSetting>();
        IPage<AvatarsSetting> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {

            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        return baseMapper.selectPage(page, query);
    }

    @Override
    public AvatarsSetting create(AvatarsSetting param) {
        val avatarssetting = new AvatarsSetting();
        BeanUtils.copyProperties(param, avatarssetting);
        val saveFlag = save(avatarssetting);
        return saveFlag ? avatarssetting : null;
    }

    @Override
    public AvatarsSetting get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public AvatarsSetting getByCompanyIdAndAvatarId(Long companyId, Long avatarId) {
        return baseMapper.selectOne(new QueryWrapper<AvatarsSetting>().eq(AvatarsSetting.COMPANY_ID, companyId).eq(AvatarsSetting.AVATAR_ID, avatarId));
    }

    @Override
    public AvatarsSetting update(Long id, AvatarsSetting param) {
        val avatarssetting = new AvatarsSetting();
        BeanUtils.copyProperties(param, avatarssetting);
        avatarssetting.setId(id);
        val updateFlag = updateById(avatarssetting);
        return updateFlag ? avatarssetting : null;
    }


    @Override
    public AvatarsSetting convertVo(AvatarsSetting avatarssetting) {
        if (avatarssetting == null) {
            return null;
        }
        val vo = new AvatarsSetting();
        BeanUtils.copyProperties(avatarssetting, vo);
        return vo;
    }

    @Override
    public List<AvatarsSetting> convertVo(List<AvatarsSetting> AvatarsSettingList) {
        if (CollUtil.isNotEmpty(AvatarsSettingList)) {
            return AvatarsSettingList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
